<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2023-05-08 10:55:00
// +----------------------------------------------------------------------

namespace app\manage\controller\admin;

use app\common\model\Config;
use buwang\base\AdminBaseController;


/**
 * @ControllerAnnotation(title="存储设置控制器")
 */
class Storage extends AdminBaseController
{
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * @notes 获取存储引擎列表
     * @return \think\Response
     */
    public function lists()
    {
        return $this->success('获取成功', Config::lists());
    }


    /**
     * @notes 存储配置信息
     * @return \think\Response
     */
    public function detail()
    {
        $param = $this->request->get();
        if (!isset($param['engine'])) {
            return $this->error('参数不正确');
        }
        $params = [
            'engine' => $param['engine'],
        ];
        return $this->success('获取成功', Config::detail($params));
    }

    /**
     * @notes 存储配置基础信息
     * @return \think\Response
     */
    public function base()
    {
        if ($this->request->get()) {
            $param = $this->request->get();
            if (!isset($param['type']) || !isset($param['name'])) {
                return $this->error('参数不正确');
            }
            return $this->success('获取成功', Config::getConfigData($param['type'], $param['name']));
        } else {
            $param = $this->request->post();
            if (!isset($param['type'])) {
                return $this->error('参数不正确');
            }
            Config::setConfigData('storage', 'base', [
                'upload_allow_ext' => $param['upload_allow_ext'] ?? '',
                'upload_allow_size' => $param['upload_allow_size'] ?? '',
            ]);
            return $this->success('保存成功');
        }

    }


    /**
     * @notes 设置存储参数
     * @return \think\Response
     */
    public function setup(): \think\Response
    {
        $param = $this->request->post();
        if (!isset($param['engine']) || !isset($param['status'])) {
            return $this->error('参数不正确');
        }
        $result = Config::setup($param);
        if ($result === true) {
            return $this->success('配置成功');
        }
        if ($result == 'default') {
            return $this->success('默认开启本地存储');
        }
        return $this->error('配置失败');
    }


    /**
     * @notes 切换存储引擎
     * @return \think\Response
     */
    public function change()
    {
        $param = $this->request->post();
        if (!isset($param['engine'])) {
            return $this->error('参数不正确');
        }
        $params = [
            'engine' => $param['engine'],
        ];
        Config::change($params);
        return $this->success('切换成功');
    }

}